package com.showcai.service.impl;

import com.showcai.dao.CertificatePackMapper;
import com.showcai.module.Certificate;
import com.showcai.module.CertificatePack;
import com.showcai.module.User;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.result.ReturnCode;
import com.showcai.service.CertificatePackService;
import com.showcai.service.CertificateService;
import com.showcai.service.ServiceBase;
import com.showcai.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zpc on 2017/7/11.
 */

@Service
public class CertificatePackServiceImpl extends ServiceBase implements CertificatePackService {

    @Autowired
    CertificatePackMapper certificatePackMapper;
@Autowired
    CertificateService certificateService;
@Autowired
    UserService userService;
    @Override
    public List<Certificate> getCertificateListFromPackByMemberId(String memberId) throws MyRunTimeExcption {

       User user= userService.getUserByThirdId(memberId);
       if(user==null){
           throw new MyRunTimeExcption(ReturnCode.USER_IS_INEXISTENCE);
       }

        return certificateService.getCertificateListFromPackByUserId(user.getTid());
    }

    private void addCertificatePack(CertificatePack certificatePack) {
        certificatePackMapper.insertCertificatePack(certificatePack);
    }

    private void updateCertificatePack(CertificatePack certificatePack) {
        certificatePackMapper.updateByPrimaryKeySelective(certificatePack);
    }

    @Override
    public void modifyCertificatePack(CertificatePack certificatePack) throws MyRunTimeExcption {
        if(certificatePack.getTid()==null){
            addCertificatePack(certificatePack);
            return;
        }
        updateCertificatePack(certificatePack);
    }

    @Override
    public CertificatePack getCertificateListFromPackByCertificateId(String certificateId) throws MyRunTimeExcption {
        return certificatePackMapper.selectCertificatePackByCertificateId(certificateId);
    }
}
